package com.fineaiops.gateway.service;

import com.alibaba.fastjson.JSONObject;
import com.fineaiops.gateway.bean.ServiceNode;
import com.fineaiops.gateway.service.mapper.ServiceNodeMapper;
import com.fineaiops.gateway.util.JsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;


@Service
public class NodeService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void query() {
        System.out.println(jdbcTemplate.queryForList("select * from service_traffic"));
    }

    public List<ServiceNode> getAllServiceNode() {
        String sql = "SELECT * FROM service_status AS s LEFT JOIN service_traffic AS t ON s.id=t.id ";
        List<ServiceNode> res = jdbcTemplate.query(sql, new ServiceNodeMapper());
        return res;
    }
    public ServiceNode getServiceByID(String id) {
        String sql = "SELECT * FROM service_status AS s LEFT JOIN service_traffic AS t ON s.id=t.id WHERE s.id = ?";
        ServiceNode res = jdbcTemplate.queryForObject(sql, new ServiceNodeMapper(), id);
        return res;
    }

    public List<JSONObject> getServiceEdges() {
        String sql = "SELECT MAX(source_service_id) AS source, MAX(dest_service_id) AS dest, entity_id " +
                        "FROM service_relation_server_side " +
                        "GROUP BY entity_id";
        RowMapper<JSONObject> mapper = (rs, rowNum) -> {
            JSONObject object = new JsonBuilder()
                    .put("source", rs.getString("source"))
                    .put("dest", rs.getString("dest"))
                    .build();
            return object;
        };
        List<JSONObject> res = jdbcTemplate.query(sql, mapper);
        sql = "SELECT MAX(source_service_id) AS source, MAX(dest_service_id) AS dest, entity_id " +
                "FROM service_relation_client_side " +
                "GROUP BY entity_id";
        res.addAll(jdbcTemplate.query(sql, mapper));
        return res;
    }

    public TreeMap<String, ArrayList<String>> getRelatedService(String id) {
        TreeMap<String, ArrayList<String>> relationMap = new TreeMap<>();
        getRelatedService(id, relationMap, "source", "dest");
        getRelatedService(id, relationMap, "dest", "source");
        return relationMap;
    }

    public void getRelatedService(String id, TreeMap<String, ArrayList<String>> relationMap, String source, String dest) {
        relationMap.put(source, new ArrayList<>());
        String sourceColumn = source + "_service_id";
        String destColumn = dest + "_service_id";
        String sql1 = "SELECT distinct " + destColumn + " AS node " +
                "FROM service_relation_server_side " +
                "WHERE " + sourceColumn + " = ?";
        jdbcTemplate.query(sql1, rs -> {relationMap.get(source).add(rs.getString("node"));},
                id);
        String sql2 = "SELECT distinct " + destColumn + " AS node " +
                "FROM service_relation_client_side " +
                "WHERE " + sourceColumn + " = ?";
        jdbcTemplate.query(sql2, rs -> {relationMap.get(source).add(rs.getString("node"));},
                id);
    }

}
